import Operator from "./Operator";
import {RC} from "../config/RC";
import MessageBox from "../entities/MessageBox";
import {USER, CHAT, GROUP} from "../config/config";
import Users from "../entities/User";
import Group from "../entities/Group";
import MessageBoxForGroup from "../entities/MessageBoxForGroup";
import MessageBoxForSingleUser from "../entities/MessageBoxForSingleUser";

class SendMessageOperator extends Operator{
    public async execute(): Promise<RC> {
        const userId = this.args.getProperty('userId');
        const messageType = this.args.getProperty('messageType');
        const context = this.args.getProperty('context');
        const boxType = this.args.getProperty('boxType');
        const receiverId = this.args.getProperty('receiverId');
        const userName = this.args.getProperty('userName');
        let messageBox : MessageBox | null = null;
        if(boxType === USER){
            messageBox = new MessageBoxForSingleUser(boxType, receiverId);
            if (!messageBox) {
                return RC.USERID_NOTEXIST;
            }
            messageBox.insertNewMessage(messageType,userId,context,userName,userId);
        }else if(boxType === GROUP){
            messageBox = new MessageBoxForGroup(boxType, receiverId);
            if (!messageBox) {
                return RC.USERID_NOTEXIST;
            }
            messageBox.insertNewMessage(messageType,receiverId,context,userName,userId);
        }
        
        return RC.SUCCESS;
    }
}

export default SendMessageOperator;